package com.jsj.isdt.servlet;

import com.alibaba.fastjson.JSON;
import com.jsj.isdt.entity.Book;
import com.jsj.isdt.entity.Order;
import com.jsj.isdt.utils.DBUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

public class ServletBook extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        String action = req.getParameter("action") == null ? "" : req.getParameter("action");
        switch (action) {
            case "getdglist":
                getDglist(req, resp);
                break;
            case "getdglist1":
                getDglist1(req, resp);
                break;
            case "getdglist2":
                getDglist2(req, resp);
                break;
            case "getdglist3":
                getDglist3(req, resp);
                break;
            case "getdglist6":
                getDglist6(req, resp);
                break;
            case "getdglist5":
                getDglist5(req, resp);
                break;

            default:
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doGet(req, resp);
    }

    private void getDglist(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            PrintWriter out = resp.getWriter();
            String sql = "select * from Book";
            List<Book> list = DBUtils.testDemoQueryBeanList(sql, Book.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("total", list.size());
            map.put("rows", list);
            out.write(JSON.toJSONString(map));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
    private void getDglist1(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            PrintWriter out = resp.getWriter();
            String sql = "select bname,bprice,bpdesc,cid from Book where isHot=1";
            List<Book> list = DBUtils.testDemoQueryBeanList(sql, Book.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("total", list.size());
            map.put("rows", list);
            out.write(JSON.toJSONString(map));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
    private void getDglist2(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            PrintWriter out = resp.getWriter();
            String sql = "select bname,bprice,bpdesc,cid from Book where bargainer=1";
            List<Book> list = DBUtils.testDemoQueryBeanList(sql, Book.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("total", list.size());
            map.put("rows", list);
            out.write(JSON.toJSONString(map));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
    private void getDglist3(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            PrintWriter out = resp.getWriter();
            String sql = "select bname,bprice from Book where cid='爱情'";
            List<Book> list = DBUtils.testDemoQueryBeanList(sql, Book.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("total", list.size());
            map.put("rows", list);
            out.write(JSON.toJSONString(map));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
    private void getDglist4(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            PrintWriter out = resp.getWriter();
            String sql = "select * from Book where cid='爱情' ";
            List<Book> list = DBUtils.testDemoQueryBeanList(sql, Book.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("total", list.size());
            map.put("rows", list);
            out.write(JSON.toJSONString(map));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
    private void getDglist5(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            PrintWriter out = resp.getWriter();
            String sql = "select id,bookname,number,price from bookorder where username='张三'  ";
            List<Order> list = DBUtils.testDemoQueryBeanList(sql, Order.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("total", list.size());
            map.put("rows", list);
            out.write(JSON.toJSONString(map));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
    private  void  getDglist6 (HttpServletRequest req, HttpServletResponse resp)  {
        try {
            int  pageIndex = req.getParameter("page") ==null? 1: Integer.parseInt(req.getParameter("page"));
            int  pagesize = req.getParameter("row") ==null? 10: Integer.parseInt(req.getParameter("row"));
            String bname = req.getParameter("bname");
            PrintWriter out = resp.getWriter();
            HashMap<String, Object> map = new HashMap<>();
            String strwhere="";
            if (bname == null || bname.equals(""))
                strwhere= "";
            else
                strwhere =" where bname like '%"+bname+"%'";
            String sqlcount;
            sqlcount ="select count(*) from book" +strwhere;
            int total = Integer.parseInt(DBUtils.QueryScalar(sqlcount).toString());
            map.put("total", total);
            String sqllist = "select * from book "+strwhere+" limit "+(pageIndex-1)*pagesize+','+pagesize;
            List<Book> list = DBUtils.testDemoQueryBeanList(sqllist, Book.class);
            map.put("rows", list);
            out.write(JSON.toJSONString(map));
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}